import { Component, OnInit,Output,EventEmitter,Input } from '@angular/core';
import {downIn} from "../../../../../animate";
import { RollService} from "../../../../../my-service/roll.service";
import { NzMessageService } from 'ng-zorro-antd';
import {UtilService} from "../../../../../my-service/util.service";

@Component({
  selector: 'app-add-notice',
  templateUrl: './add-notice.component.html',
  styleUrls: ['./add-notice.component.css'],
  animations:[downIn]
})
export class AddNoticeComponent implements OnInit {
  @Output() onCloseAddNotice = new EventEmitter<boolean>();
  @Input()  rollGuid:string;//班级guid
  @Input() notice:any;
  addNotice = {
    userGuid:null,//用户Guid
    title:'',
    content: null,
    rollGuid:'',
    selectStuArr:[],//选择发布的学生
    selectAll:false,//选择全部学生
    studentArr:[],//班级里的所有学生
    selectTatol:0,//主要用来控制是否进行全选
    noticeGuid:'',//修改时需要的Guid
    className:'',//班级名称
    imgUpload:null,//公告图片上传地址
    addStatus:0,//判断用户添加状态
    SelectAll:function (event) {
      this.studentArr.forEach(item=>{
        item.isSelect = event
      });
    },
    countSelect:function (event,index) {
      this.selectTatol =0;
      this.studentArr[index].isSelect = event;
      this.studentArr.forEach(item=>{
        if(item.isSelect){
         this.selectTatol+=1;
        }
      });
      if(this.selectTatol == this.studentArr.length){
        this.selectAll = true;
      }else {
        this.selectAll = false;
      }
    },
    _constructorSelect:function () {
      if(this.studentArr.length ==this.selectStuArr.length ){
        this.selectAll = true;
        this.selectTatol = this.selectStuArr.length;
        this.studentArr.forEach(item=>{
          item.isSelect = true;
        });
      }else {
        for (let i = 0; i < this.selectStuArr.length; i++) {
          for (let  j = 0; j < this.studentArr.length; j++) {
            if(this.selectStuArr[i].PK_UserGuid ==this.studentArr[j].PK_UserGuid){
              this.studentArr[j].isSelect = true;
              continue ;
            }
          }
        }
      }
    }
  };
 getUserGuid(){
   this.utileServe.getUserInfo().then(res=>{
     if(res.status==1){
       this.addNotice.userGuid = res.userGuid;
       this.addNotice.imgUpload = this.utileServe.loodImgUrl +`/?directory=Roster/Roster/notice&userGuid=${this.addNotice.userGuid}`
     }
   })
 }
  constructor(
 private rollServe:RollService,
 private msg :NzMessageService,
 private utileServe:UtilService
  ) {
    this.getUserGuid();
  }
 getSudentList () {
    this.rollServe.getStudentlist(this.rollGuid).then(res=>{
      if(res.status == 1){
        this.addNotice.studentArr =res.data;
        this.addNotice.studentArr.forEach((item)=>{
          item.isSelect = false;
        });
      }else {
        console.log(res);
        this.msg.error('服务器内部错误，请刷新重试');
      }
    })
 }
 //获取学生列表
  getClassInfo(classGuid) {
    this.rollServe.getClassInfo(classGuid).then(res=>{
      if(res.status == 1){
        this.addNotice.className = res.data[0].ClassName;
      }else {
        console.log(res);
        this.msg.error('服务器内部错误，请刷新重试');
      }
    })
  }
  //获取班级信息
getSelectStuList(noticeid){
    this.rollServe.getNoticeStudentList(noticeid).then(res=>{
      if(res.status==1){
        this.addNotice.selectStuArr = res.data;
        this.addNotice._constructorSelect();
      }else {
        console.log(res);
        this.msg.error('服务器内部错误，请刷新重试');
      }
    })
}
  ngOnInit() {
    this.addNotice.rollGuid = this.rollGuid;
    this.getSudentList();
    if (this.notice){
      this.addNotice.title = this.notice.Title;
      this.addNotice.noticeGuid = this.notice.PK_NoticeGuid;
      this.addNotice.content =this.notice.Content;
      this.getSelectStuList( this.addNotice.noticeGuid);
    }
    this.getClassInfo( this.addNotice.rollGuid );
  }
  onClose () {
    this.addNotice = null;
    this.rollGuid = '';
    this.notice = null;
    //清空引用
    this.onCloseAddNotice.emit(false);
  }
  getJSON (event) {
    this.addNotice.content =event;
  }
  uploadNotice () {
    this.addNotice.selectStuArr=[];
    if(this.addNotice.addStatus!==0){
      return;
    }
    this.addNotice.studentArr.forEach(item=>{
      if(item.isSelect){
        this.addNotice.selectStuArr.push(item);
      }
    });
    if (!this.addNotice.title){
      this.msg.warning('公告标题不能为空，请填写');
      return
    }else if (this.addNotice.selectStuArr.length==0){
      this.msg.warning('请您选择该公告发向的学生');
      return
    }
    else {
      this.addNotice.addStatus++;
      let data = {title:this.addNotice.title,content:this.addNotice.content,classGuid:this.addNotice.rollGuid,object:this.addNotice.selectStuArr};
      if(this.notice){
        this.rollServe.updateNotice(this.addNotice.noticeGuid,data).then(res=>{
          if(res.status == 1){
             this.msg.success('公告修改成功');
              this.onClose();
          }else {
            console.log(res);
            this.msg.error('服务器内部错误，请刷新重试');
          }
        })
      }else {
        this.rollServe.uploadNotice(data).then(res=>{
          if(res.status == 1){
            this.msg.success('公告创建成功');
              this.onClose();
          }else {
            console.log(res);
            this.msg.error('服务器内部错误，请刷新重试');
          }
        })
      }
    }
  }

}
